<template>
  <div class="container">
    <div id="box" class="box">
      <div class="img">
        <img src="../assets/vue.svg" alt="">
      </div>
      <!-- 编写form el-form 表单整体  rules 需要是一个对相关,对象的字段和model的字段要一样 -->
      <el-form ref="ruleFormRef" hide-required-asterisk :model="loginForm" :rules="rules" label-width="80px">
        <!-- 子类 -->
        <el-form-item prop="username" label="用户名">
          <!-- prefix-icon 前边添加图标 -->
          <!-- suffix-icon 后边添加图标 -->
          <el-input :prefix-icon="UserFilled" v-model="loginForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type="password" :prefix-icon="Lock" v-model="loginForm.password"></el-input>
        </el-form-item>
        <!-- 提交按钮 -->
        <el-form-item>
          <el-button type="primary" @click="submitForm">登录</el-button>
          <el-button>取消</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<!-- 没有运行之前,就可以察觉错误 -->
<script setup lang="ts">
// 图标
import { Lock, UserFilled } from '@element-plus/icons-vue'
import type { FormRules } from 'element-plus'
import { reactive, ref } from 'vue'
import request from '@/util/request'
import { ElMessage } from 'element-plus'
import { useRouter } from 'vue-router'

import type { userp } from '@/api/user/type'
import type { responseUserData } from '@/api/login/type'

const loginForm = reactive<userp>({
  username: 'admin',
  password: '123456',
})
const rules = reactive<FormRules>({
  username: [
    { required: true, message: '用户名必填', trigger: 'blur' },
    { min: 3, max: 5, message: '3到5个字符的长度', trigger: 'blur' },
  ],
  password: [
    { required: true, message: '密码必填', trigger: 'blur' },
    { pattern: /\d{6}/, message: '密码纯数字6位', trigger: 'blur' },
  ],
})
// dom元素,名字必须和ref属性名字一样
const ruleFormRef = ref()
// 路由对象
const router = useRouter()
// 登录的方法
const submitForm = () => {
  ruleFormRef.value.validate(async (istrue: any) => {
    // 如果是true 代表校验成功
    if (!istrue) {
      return ElMessage.error('请正确填写表单')
    }
    // response的类型
    const { data, meta }: responseUserData = await request({
      data: loginForm,
      url: '/login',
      method: 'POST',
    })
    sessionStorage.setItem("token", data.token)
    console.log(data.token)
    if (meta.status !== 200) {
      return ElMessage.error('登录失败')
    }
    router.push('/home')
    ElMessage.success('登录成功')
  })
}
</script>

<style scoped lang="scss">
.container {
  height: 100vh;
  background-color: #2b4b6b;
  display: flex;
  justify-content: center;
  align-items: center;

  .box {
    position: relative;
    left: 0px;
    top: 0px;
    width: 450px;
    height: 300px;
    border: 1px solid transparent;
    border-radius: 10px;
    background-color: #fff;

    .img {
      position: absolute;
      left: 50%;
      top: -50%;
      border-radius: 50%;
      overflow: hidden;
      border: 10px solid rgba(0, 0, 0, 0.1);
      box-shadow: 0 0 5px 5px rgba(255, 255, 255, 0.9);
      transform: translate(-50%, 50%);

      img {
        width: 130px;
        height: 130px;
        background-color: #eeeeee;
      }
    }

    .el-form {
      margin-top: 100px;
    }

    .el-form-item {
      width: 400px;
    }
  }
}
</style>
